<template>
    <div class="form">
        <el-form ref="form" :model="obj.data" label-width="0">
            <el-form-item label="" prop="file" :rules="[formRulesMixin.notNull]">
                <el-upload
                    :auto-upload="false"
                    class="upload-demo"
                    drag
                    multiple
                    :accept="['application/pdf','application/vnd.openxmlformats-officedocument.wordprocessingml.document']"
                    v-model:file-list="obj.data.file"
                    :on-change="handleChange"
                >
                    <el-icon class="el-icon--upload"><UploadFilled /></el-icon>
                    <div class="el-upload__text flex-column">
                        <span>拖放文件置此处 或 <em>点击上传</em></span>
                        <span style="font-size: 12px;color: #999;">目前支持上传Docx、PDF文件类型</span>
                    </div>
                </el-upload>
            </el-form-item>
        </el-form>
        <div class="btns">
            <el-button @click="$emit('close')">取消</el-button>
            <el-button type="primary" @click="upData">提交</el-button>
        </div>
    </div>
</template>

<script setup>
    import { reactive, getCurrentInstance, computed } from 'vue'
    import { UploadFilled } from '@element-plus/icons-vue'
    import UpfileSingle from '@/components/upfile_single.vue'
    let { proxy } = getCurrentInstance()
    const emit = defineEmits(['init', 'close']) //此处要完全对应，采用固定的update:prop属性
    const props = defineProps({
        data: { type: Object, default: () => { return {}}},
    })

    let obj = reactive({
        data: {
            file: [],
            purpose: 'retrieval',
            knowledge_id: props.data.id||'',
        }
    })

    const handleChange=(e)=>{
        console.log(e); 
    }

    let upData = () => {
        proxy.$refs['form'].validate(valid => {
            if (valid) {
                obj.data.file.map((e)=>{
                    up(e.raw)
                })
            } else {
                return false
            }
        })
    }

    const up=(formData)=>{
        proxy.$ajax({
            baseURL: 'https://open.bigmodel.cn/api/paas/v4/files',
            url: '',
            method: 'post',
            isUploadFile: true,
            progress: true,
            data: {
                file: formData,
                purpose: 'retrieval',
                knowledge_id: proxy.$route.query.id||'',
            },
            isResovle: true,
        }).then(data => {
            if(data&&data.successInfos&&data.successInfos.length>0){
                proxy.elMessageMixin(`成功`, 'success')
                proxy.$emit('close')
                proxy.$emit('init')
            }
        }).catch(() => {})
    }
</script>

<style scoped>
.upload-demo{width: 100%;}
</style>

